﻿/* $RCSFile: HeaderTitle.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/HeaderTitle.cs $
 * $log$
 * Revision 19 2011/02/22 01:23:08  christian.surieux
 *   V4 Migration 22/2/11
 * Revision 19 2011/02/22 01:22:16  christian.surieux
 *   V4 Migration 22/2/11
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Core;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.Configuration;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.UI.WebControls
{

	public class HeaderTitle: System.Web.UI.WebControls.PlaceHolder
	{
		Label Title				        =	new Label();
		private bool _dataBindOnInit	=   true;

		/// <summary>
		/// Should it bind in OnInit
		/// </summary>
		[DefaultValue(true)]
		public bool DataBindOnInit
		{
			get
			{
				return _dataBindOnInit;
			}
			set
			{
				_dataBindOnInit = value;
			}
		}

		[DefaultValue(true)]
		bool _ShowSiteTitle =   true;
		public bool ShowSiteTitle
		{
			get
			{
				return _ShowSiteTitle;
			}
			set
			{
				_ShowSiteTitle = value;
			}
		}

		public string CssClass
		{
			get
			{
				if ( Title != null )
					return Title.CssClass;
				return string.Empty;
			}
			set
			{
				if ( Title != null )
					Title.CssClass	=	value;
			}
		}

		public string Text
		{
			get
			{
				if ( Title != null )
					return Title.Text;
				return string.Empty;
			}
			set
			{
				if ( Title != null )
					Title.Text	=	value;
			}
		}

		public HeaderTitle()
		{
			EnableViewState     =   false;
		}

		protected override void OnInit(EventArgs e)
		{
			if (WebControlsTraceSwitch.Sw.TraceVerbose)
				Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HeaderTitle OnInit BEG DataBindOnInit: {0}",DataBindOnInit),WebControlsTraceSwitch.Sw.Info);
			if ( PortalSettings.IsUnderCRM )
				Visible         =   false;
			else if( DataBindOnInit )
					DataBind();
			base.OnInit(e);
			if (WebControlsTraceSwitch.Sw.TraceVerbose)
				Trace2.WriteLine( "[V]HeaderTitle OnInit END.",WebControlsTraceSwitch.Sw.Info);
		}
		
		protected override void CreateChildControls()
		{
			if (WebControlsTraceSwitch.Sw.TraceVerbose)
				Trace2.WriteLine(string.Format(CultureInfo.InvariantCulture, "[V]HeaderTitle CreateChildControls BEG DesignMode: {0}, ShowSiteTitle: {1}.", 
					DesignMode,ShowSiteTitle), WebControlsTraceSwitch.Sw.Info);
			Controls.Clear();
			HtmlTable Tb		                =	new HtmlTable();
			Tb.CellPadding		                =	0;
			Tb.CellSpacing		                =	0;
			HtmlTableRow rw		                =	new HtmlTableRow();
			HtmlTableCell c1	                =	new HtmlTableCell();
			HtmlTableCell c2	                =	new HtmlTableCell();
			if( !DesignMode )
			{
				PortalSettings portalSettings   =   (PortalSettings) HttpContext.Current.Items[StStr.strPortalSettings];
				// Dynamically Populate the Portal Site Name
				if ( ShowSiteTitle )
					Title.Text			        =   portalSettings.PortalName;
				Title.Visible                   =   ShowSiteTitle;
				// if securemode, and if a secure string exist, insert it
				if ( Page.Request.IsSecureConnection &&
					portalSettings.CustomSettings.ContainsKeyNotEmpty(StStr.SiteSettingsSecuredByScript) )
				{
					Literal LitCtl	            =	new Literal();
					LitCtl.Text		            =	portalSettings.CustomSettings[StStr.SiteSettingsSecuredByScript].Value;
					c2.Controls.Add(LitCtl);
				}
			}
			if ( ShowSiteTitle )
			{
				c1.Controls.Add(Title);
				rw.Cells.Add(c1);
			}
			rw.Cells.Add(c2);
			Tb.Rows.Add(rw);
			Controls.Add(Tb);
			ChildControlsCreated    =   true;
			if (WebControlsTraceSwitch.Sw.TraceVerbose)
				Trace2.WriteLine( "[V]HeaderTitle CreateChildControls END.",WebControlsTraceSwitch.Sw.Info);
		}
	}
}